package com.elitel.common.auth.config;


import com.elitel.common.auth.AuthProperties;
import com.elitel.common.auth.filter.CustomAuthenticationFilter;
import org.jasig.cas.client.configuration.ConfigurationKeys;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.http.HttpSessionListener;

/**
 * @author hi_bj
 */
@Configuration
@ConditionalOnProperty(prefix = "auth", name = "type", havingValue = "cas")
public class CasConfig {

    @Resource
    private AuthProperties authProperties;

    @Bean
    public ServletListenerRegistrationBean<HttpSessionListener> httpSessionListener() {
        ServletListenerRegistrationBean<HttpSessionListener> listener = new ServletListenerRegistrationBean();
        listener.setListener(new SingleSignOutHttpSessionListener());
        return listener;
    }

    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new SingleSignOutFilter());
        filterRegistrationBean.addUrlPatterns("/*");

        filterRegistrationBean.addInitParameter(ConfigurationKeys.CAS_SERVER_URL_PREFIX.getName(), authProperties.getCenter().getServerLogoutUrl());
        filterRegistrationBean.addInitParameter(ConfigurationKeys.SERVER_NAME.getName(), authProperties.getServerName());
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean ticketValidationFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        filterRegistrationBean.addUrlPatterns("/*");

        filterRegistrationBean.addInitParameter(ConfigurationKeys.CAS_SERVER_URL_PREFIX.getName(), authProperties.getCenter().getServerUrlPrefix());
        filterRegistrationBean.addInitParameter(ConfigurationKeys.SERVER_NAME.getName(), authProperties.getServerName());
        //默认正则表达式
        filterRegistrationBean.addInitParameter(ConfigurationKeys.IGNORE_PATTERN.getName(), authProperties.getRegExcludeUrl());
        filterRegistrationBean.addInitParameter(ConfigurationKeys.ENCODING.getName(),"utf-8");
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean authenticationFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter();
        customAuthenticationFilter.setCenterRedirectUrl(authProperties.getCenterRedirectUrl());
        filterRegistrationBean.setFilter(customAuthenticationFilter);
        filterRegistrationBean.addUrlPatterns("/*");

        filterRegistrationBean.addInitParameter(ConfigurationKeys.CAS_SERVER_LOGIN_URL.getName(), authProperties.getCenter().getServerLoginUrl());
        filterRegistrationBean.addInitParameter(ConfigurationKeys.SERVER_NAME.getName(), authProperties.getServerName());
        //默认正则表达式
        filterRegistrationBean.addInitParameter(ConfigurationKeys.IGNORE_PATTERN.getName(), authProperties.getRegExcludeUrl());
        filterRegistrationBean.addInitParameter(ConfigurationKeys.ENCODING.getName(),"utf-8");
        filterRegistrationBean.setOrder(3);
        return filterRegistrationBean;
    }
}
